package optimizers.ganeat.report;

import util.MyUtil;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import fitness.OscillatorBasedResult;
import fitness.TriangleFitness;

import optimizers.commons.GAConstants;
import optimizers.commons.RunConfig;
import optimizers.ganeat.Connection;
import optimizers.ganeat.Constants;
import optimizers.ganeat.Individual;
import optimizers.ganeat.Species;

import visualization.GraphDisplayer;
import visualization.PlotExpData;
import visualization.PlotIndividualHistory;
import visualization.ganeat.PlotSpeciesData;

public class ReaderNEAT2 {

	/**
	 * @param args
	 * @throws ClassNotFoundException
	 */
	public static void openReport(File file) throws ClassNotFoundException {
		FileInputStream saveFile;
		try {
			Individual[] list = new Individual[200];
			for (int i = 0; i < list.length; i++) {
				list[i] = null;
			}

			int count = 0;
			for (File fileEntry : file.listFiles()) {
				if (fileEntry.isDirectory()) {
					System.out.println(fileEntry);
					saveFile = new FileInputStream(fileEntry + "\\log");
					ObjectInputStream restore = new ObjectInputStream(saveFile);
					ArrayList<ArrayList<Species>> speciesVisualData = (ArrayList<ArrayList<Species>>) restore
							.readObject();
					try {

						/*
						 * JFrame graphFrame1 = new
						 * JFrame("Species Visualization");
						 * graphFrame1.setSize(1500, 800); GridLayout grid1 =
						 * new GridLayout(0, 1); JPanel panel1 = new JPanel();
						 * panel1.setLayout(grid1); grid1.setColumns(1);
						 * graphFrame1.add(panel1); PlotSpeciesData demo4 = new
						 * PlotSpeciesData(speciesVisualData, 1500, 800);
						 * panel1.add(demo4); graphFrame1.setVisible(true);
						 */double[][] speciesData = (double[][]) restore
								.readObject();
						Map<String, Integer> innovations = (Map<String, Integer>) restore
								.readObject();
						Map<String, String> nodeNames = (Map<String, String>) restore
								.readObject();
						ArrayList<Individual> speciesLib = (ArrayList<Individual>) restore
								.readObject();
						RunConfig config = (RunConfig) restore.readObject();
						/*
						 * PlotExpData demo = MyUtil.getSpeciesFitnessPlot(
						 * speciesVisualData, demo4.getIndivColor());
						 * PlotExpData demo3 = new PlotExpData(
						 * "Number of species over generations", speciesData,
						 * new String[] { "nSpecies", "Speciation Threshold" },
						 * "Generation", "Number of species"); JFrame graphFrame
						 * = new JFrame("Metrics over generations");
						 * graphFrame.setSize(1200, 800); GridLayout grid = new
						 * GridLayout(0, 1); JPanel panel = new JPanel();
						 * panel.setLayout(grid); grid.setColumns(1);
						 * graphFrame.add(panel); panel.add(demo);
						 * panel.add(demo3); graphFrame.setVisible(true);
						 */
						for (int i = 0; i < speciesVisualData.size(); i++) {
							ArrayList<Species> speciesList = speciesVisualData
									.get(i);
							for (Species species : speciesList) {
								for (Individual individual : species.individuals) {
									count++;
									int complexity = individual
											.getNEnabledConnections();
									if (individual.fitnessResult != null) {
										System.out.println(complexity
												+ " "
												+ individual.fitnessResult
														.finalResult());
										if (list[complexity] == null
												|| list[complexity].fitnessResult
														.finalResult() < individual.fitnessResult
														.finalResult()) {
											list[complexity] = individual;
										}
									}
								}
							}
						}
					} catch (Exception ex) {
						ex.printStackTrace();
					}
					restore.close();
				}
			}
			for (int i = 0; i < list.length; i++) {
				if (list[i] != null) {
					System.out.println(i + " "
							+ list[i].fitnessResult.finalResult());
					JFrame frame = new JFrame("Complexity: " + i);
					frame.setSize(1024, 768);
					frame.setVisible(true);
					JPanel panelIndiv = new JPanel();
					frame.add(panelIndiv, BorderLayout.CENTER);
					panelIndiv.setLayout(new BorderLayout(0, 0));
					GraphDisplayer bestIndividuals = new GraphDisplayer(
							panelIndiv.getSize().width,
							panelIndiv.getSize().height);
					panelIndiv.add(bestIndividuals, BorderLayout.CENTER);
					bestIndividuals.addIndividual(list[i]);
					panelIndiv.revalidate();
				}
			}
			System.out.println("Total: " + count);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws ClassNotFoundException,
			IOException {
		// TODO Auto-generated method stub

		// JFileChooser fc = new JFileChooser();
		// int returnVal;
		// do {
		// returnVal = fc.showOpenDialog(null);
		// } while (returnVal != JFileChooser.APPROVE_OPTION
		// && returnVal != JFileChooser.CANCEL_OPTION);
		// File file = fc.getSelectedFile();
		File file = new File("C:\\Users\\Huy\\Documents\\math_gaussian");
		System.out.println(file.toString());
		/*
		 * FileInputStream saveFile = new FileInputStream(file);
		 * ObjectInputStream restore = new ObjectInputStream(saveFile);
		 * ArrayList<ArrayList<Species>> speciesVisualData =
		 * (ArrayList<ArrayList<Species>>) restore .readObject(); JPanel panel =
		 * new JPanel(); JFrame frame = new JFrame(""); frame.add(panel);
		 * frame.setSize(1200, 800); Individual bestIndividual = null; for
		 * (Species sp : speciesVisualData.get(speciesVisualData.size() - 1)) {
		 * if (bestIndividual == null || bestIndividual.getFitness() <
		 * sp.getBestIndividual() .getFitness()) { bestIndividual =
		 * sp.getBestIndividual(); } } PlotIndividualHistory demo = new
		 * PlotIndividualHistory("", bestIndividual); panel.add(demo);
		 * frame.setVisible(true);
		 */
		openReport(file);
	}
}
